﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;
namespace Calculator
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        //    if (!IsPostBack)
        //    {
        //        txtInput.Text = (string)Session["Input"];
        //    }
        }
        protected void btn1_Click(object sender, EventArgs e)
        {
            if (!checkIsNew())
            {
                txtInput.Text = changeZeroFirst(txtInput.Text);
                txtInput.Text = txtInput.Text + "1";
            }
            else
            {
                txtInput.Text = "1";
            }
        }

        protected void btn2_Click(object sender, EventArgs e)
        {
            if (!checkIsNew())
            {
                txtInput.Text = changeZeroFirst(txtInput.Text);
                txtInput.Text = txtInput.Text + "2";
            }
            else
            {
                txtInput.Text = "2";
            }
        }

        protected void btn3_Click(object sender, EventArgs e)
        {
            if (!checkIsNew())
            {
                txtInput.Text = changeZeroFirst(txtInput.Text);
                txtInput.Text = txtInput.Text + "3";
            }
            else
            {
                txtInput.Text = "3";
            }
        }

        protected void btn4_Click(object sender, EventArgs e)
        {
            if (!checkIsNew())
            {
                txtInput.Text = changeZeroFirst(txtInput.Text);
                txtInput.Text = txtInput.Text + "4";
            }
            else
            {
                txtInput.Text = "4";
            }
        }

        protected void btn5_Click(object sender, EventArgs e)
        {
            if (!checkIsNew())
            {
                txtInput.Text = changeZeroFirst(txtInput.Text);
                txtInput.Text = txtInput.Text + "5";
            }
            else
            {
                txtInput.Text = "5";
            }
        }

        protected void btn6_Click(object sender, EventArgs e)
        {
            if (!checkIsNew())
            {
                txtInput.Text = changeZeroFirst(txtInput.Text);
                txtInput.Text = txtInput.Text + "6";
            }
            else
            {
                txtInput.Text = "6";
            }
        }

        protected void btn7_Click(object sender, EventArgs e)
        {
            if (!checkIsNew())
            {
                txtInput.Text = changeZeroFirst(txtInput.Text);
                txtInput.Text = txtInput.Text + "7";
            }
            else
            {
                txtInput.Text = "7";
            }
        }

        protected void btn8_Click(object sender, EventArgs e)
        {
            if (!checkIsNew())
            {
                txtInput.Text = changeZeroFirst(txtInput.Text);
                txtInput.Text = txtInput.Text + "8";
            }
            else
            {
                txtInput.Text = "8";
            }
        }

        protected void btn9_Click(object sender, EventArgs e)
        {
            if (!checkIsNew())
            {
                txtInput.Text = changeZeroFirst(txtInput.Text);
                txtInput.Text = txtInput.Text + "9";
            }
            else
            {
                txtInput.Text = "9";
            }
        }

        protected void btn0_Click(object sender, EventArgs e)
        {
            if (!checkIsNew())
            {
                txtInput.Text = txtInput.Text + "0";
            }
            else
            {
                txtInput.Text = "0";
            }
        }

        protected void btnDot_Click(object sender, EventArgs e)
        {
            if (txtInput.Text != "")
            {
                txtInput.Text = txtInput.Text + ".";
            }
        }

        protected void btnEqual_Click(object sender, EventArgs e)
        {
            string[] array = Regex.Split(txtInput.Text, "\n");
            double number1;
            double number2;
            if (double.TryParse(array[0],out number1) && double.TryParse(array[2],out number2))
            {
                switch (array[1])
                {
                    case "+":
                        txtInput.Text = "= " + (number1 + number2).ToString();
                        break;
                    case "-":
                        txtInput.Text = "= " + (number1 - number2).ToString();
                        break;
                    case "*":
                        txtInput.Text = "= " + (number1 * number2).ToString();
                        break;
                    case "/":
                        txtInput.Text = "= " +(number1 / number2).ToString();
                        break;
                    default:
                        return;
                }
            }
        }

        protected void btnDivien_Click(object sender, EventArgs e)
        {
            if (txtInput.Text != "")
            {
                txtInput.Text = txtInput.Text + "\n/\n";
            }
        }

        protected void btnMulti_Click(object sender, EventArgs e)
        {
            if (txtInput.Text != "")
            {
                txtInput.Text = txtInput.Text + "\n*\n";
            }
        }

        protected void btnMinus_Click(object sender, EventArgs e)
        {
            if (txtInput.Text != "")
            {
                txtInput.Text = txtInput.Text + "\n-\n";
            }
        }

        protected void btnSum_Click(object sender, EventArgs e)
        {
            if (txtInput.Text != "")
            {
                txtInput.Text = txtInput.Text + "\n+\n";
            }
        }
        protected string changeZeroFirst(string number)
        {
            if (number != "")
            {
                if (checkZeroFirst(number))
                {
                    number = "";
                }
            }
            return number;
        }
        protected bool checkZeroFirst(string number)
        {
            if (number.Length > 1)
            {
                if (number[0] == '0' && number[1] == '.')
                    return false;
            }
            if (number[0] == '0')
                return true;
            return false;
        }
        protected bool checkIsNew()
        {
            if(txtInput.Text != "")
            {
                if (txtInput.Text[0] == '=')
                {
                    return true;
                }
            }
            return false;
        }
    }
}
